{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cd013ba9",
   "metadata": {},
   "source": [
    "-------\n",
    "* 2022/10/31 / week11\n",
    "* Python学习记录\n",
    "* 记录人：许丹 \n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7cc1e94",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "* 元组、集合和字典的基本概念和常见方法\n",
    "> 1.元组  \n",
    "> 2.集合  \n",
    "> 3.字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "904548b1",
   "metadata": {},
   "source": [
    "## 1.元组(tuple)\n",
    ">1. 概念  \n",
    ">> 1.具有和序列数据相同的运算规则：取值和切片  \n",
    ">> 2.表征：(12345,54321,'hello!'),用圆括号括起来的，中间仍是用逗号隔开的数据形态   \n",
    "> 2.性质：**不可变性**，没有什么增删改的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "df109358",
   "metadata": {},
   "outputs": [],
   "source": [
    "product = (\"head first python\",88)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "47791e5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('head first python', 88) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(product,type(product))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "159fc1b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fc19f032",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=31, tm_hour=10, tm_min=20, tm_sec=47, tm_wday=0, tm_yday=304, tm_isdst=0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c0aec2ef",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on struct_time object:\n",
      "\n",
      "class struct_time(builtins.tuple)\n",
      " |  struct_time(iterable=(), /)\n",
      " |  \n",
      " |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      " |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      " |  sequence of 9 integers.\n",
      " |  \n",
      " |  Note that several fields' values are not the same as those defined by\n",
      " |  the C language standard for struct tm.  For example, the value of the\n",
      " |  field tm_year is the actual year, not year - 1900.  See individual\n",
      " |  fields' descriptions for details.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      struct_time\n",
      " |      builtins.tuple\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  tm_gmtoff\n",
      " |      offset from UTC in seconds\n",
      " |  \n",
      " |  tm_hour\n",
      " |      hours, range [0, 23]\n",
      " |  \n",
      " |  tm_isdst\n",
      " |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      " |  \n",
      " |  tm_mday\n",
      " |      day of month, range [1, 31]\n",
      " |  \n",
      " |  tm_min\n",
      " |      minutes, range [0, 59]\n",
      " |  \n",
      " |  tm_mon\n",
      " |      month of year, range [1, 12]\n",
      " |  \n",
      " |  tm_sec\n",
      " |      seconds, range [0, 61])\n",
      " |  \n",
      " |  tm_wday\n",
      " |      day of week, range [0, 6], Monday is 0\n",
      " |  \n",
      " |  tm_yday\n",
      " |      day of year, range [1, 366]\n",
      " |  \n",
      " |  tm_year\n",
      " |      year, for example, 1993\n",
      " |  \n",
      " |  tm_zone\n",
      " |      abbreviation of timezone name\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  n_fields = 11\n",
      " |  \n",
      " |  n_sequence_fields = 9\n",
      " |  \n",
      " |  n_unnamed_fields = 0\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "570c83fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800001, 1800002, 1800003)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_seq = 1800001,1800002,1800003\n",
    "name_seq"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c00cc583",
   "metadata": {},
   "source": [
    "## 2.集合（sets）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b822a44",
   "metadata": {},
   "source": [
    "> 1.集合的概念  \n",
    ">> 1.没有重复元素的一个数据结构  \n",
    ">> 2.形态：{'apple','banana','pear','orange','apple','banana'}  \n",
    "\n",
    "> 2.集合的运算  \n",
    ">> 1.交集运算：&，intersection  \n",
    ">> 2.并集运算：|，union"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "90ec52f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'apple', 'orange', 'pear'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple','banana','pear','orange'}\n",
    "print(basket,type(basket))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cdfc0fe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1800001,1800003,1800004,1800005,18000007}\n",
    "合唱团 = {1800009,1811111,1800004,1800001}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9e766983",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "508ff8e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7f746018",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001, 1800004}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集运算和并集运算的实践\n",
    "舞蹈社 & 合唱团"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "17768390",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001, 1800004}\n",
      "{1800001, 1800004}\n"
     ]
    }
   ],
   "source": [
    "# 交集\n",
    "print(舞蹈社 & 合唱团)\n",
    "print(舞蹈社.intersection(合唱团))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f91ecdd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001, 1800003, 1800004, 1800005, 18000007, 1811111, 1800009}\n",
      "7\n",
      "{1800001, 1800003, 1800004, 1800005, 18000007, 1811111, 1800009}\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# 并集：统计数量\n",
    "print(舞蹈社|合唱团)\n",
    "print(len(舞蹈社|合唱团))\n",
    "\n",
    "print(舞蹈社.union(合唱团))\n",
    "print(len(舞蹈社.union(合唱团)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1604a52e",
   "metadata": {},
   "source": [
    "# 3.字典（dict）\n",
    "> 1.基本概念   \n",
    ">> 1. mapping types：映射类型  \n",
    ">> 2. key:values,键：值 对 \n",
    ">> 3. 字典的性质  \n",
    ">>> 1. 无序性  \n",
    ">>> 2. key不可变性  \n",
    ">>> 3. key的唯一性  \n",
    "> 2.字典的创建  \n",
    "\n",
    "> 3.常用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2c520c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_list = [\n",
    "    [\"柠檬水\",6],\n",
    "    [\"蜜桃四季春\",10],\n",
    "    [\"芒果奶盖\",12],\n",
    "    [\"葡萄啵啵奶茶\",8],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "af09888a",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_dict = {\n",
    "    \"柠檬水\":6,\n",
    "    \"蜜桃四季春\":10,\n",
    "    \"芒果奶盖\":12,\n",
    "    \"普通啵啵奶茶\":8\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "74a6e5bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'柠檬水': 6, '蜜桃四季春': 10, '芒果奶盖': 12, '普通啵啵奶茶': 8} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(product_dict,type(product_dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "126d129d",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [24]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mproduct_dict\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "product_dict[0] #字典是无序的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "46c6d7da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_dict[\"柠檬水\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e31ce8d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_info = {\n",
    "    \"edeerewr233f\":[\"xudan\",\"IT\",\"广州\",\"广州南方学院\",\"通讯录\",\"朋友圈\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "bc835d68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xudan', 'IT', '广州', '广州南方学院', '通讯录', '朋友圈']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info[\"edeerewr233f\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40836f9c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
